package info.liuxing.condition;

import info.liuxing.bean.Blue;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

/**
 * MyImportBeanDefinitionRegistrar
 *
 * @author liuxing
 * @summary MyImportBeanDefinitionRegistrar
 * @Copyright (c) 2018, Lianjia Group All Rights Reserved.
 * @since 2018-04-09 01:30
 */
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {

    /**
     * @param importingClassMetadata 当前类的注解信息和其他信息
     * @param registry               BeanDefinition注册类
     */
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        boolean beanDefinition2 = registry.containsBeanDefinition("blue2");

        if (!beanDefinition2) {
            // 往容器中手动注册bean
            BeanDefinition rootBeanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Blue.class).getBeanDefinition();

            registry.registerBeanDefinition("blue2", rootBeanDefinition);
        }
    }
}
